package cc.admin.modules.sys.service.impl;

import cc.admin.common.api.vo.Result;
import cc.admin.common.constant.CacheConstant;
import cc.admin.common.constant.CommonConstant;
import cc.admin.common.sys.api.ISysBaseAPI;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.common.sys.vo.SysUserCacheInfo;
import cc.admin.common.util.PasswordUtil;
import cc.admin.common.util.UUIDGenerator;
import cc.admin.common.util.oConvertUtils;
import cc.admin.modules.sys.entity.*;
import cc.admin.modules.sys.mapper.*;
import cc.admin.modules.sys.model.SysDepartTreeModel;
import cc.admin.modules.sys.model.SysUserSysDepartModel;
import cc.admin.modules.sys.service.ISysUserService;
import cc.admin.modules.sys.util.FindsDepartsChildrenUtil;
import cc.admin.modules.sys.vo.SysUserDepVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <getPageFilterFields>
 * 用户表 服务实现类
 * </getPageFilterFields>
 *
 * @Author: scott
 * @Date: 2018-12-20
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

	@Autowired
	private SysUserMapper userMapper;
	@Autowired
	private SysPermissionMapper sysPermissionMapper;
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;
	@Autowired
	private SysUserDepartMapper sysUserDepartMapper;
	@Autowired
	private ISysBaseAPI sysBaseAPI;
	@Autowired
	private SysDepartMapper sysDepartMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysDepartRoleUserMapper departRoleUserMapper;
	@Autowired
	private SysDepartRoleMapper sysDepartRoleMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
	public Result<?> resetPassword(String username, String oldpassword, String newpassword, String confirmpassword) {
		SysUser user = userMapper.getUserByName(username);
		String passwordEncode = PasswordUtil.encrypt(username, oldpassword, user.getSalt());
		if (!user.getPassword().equals(passwordEncode)) {
			return Result.error("旧密码输入错误!");
		}
		if (oConvertUtils.isEmpty(newpassword)) {
			return Result.error("新密码不允许为空!");
		}
		if (!newpassword.equals(confirmpassword)) {
			return Result.error("两次输入密码不一致!");
		}
		String password = PasswordUtil.encrypt(username, newpassword, user.getSalt());
		this.userMapper.update(new SysUser().setPassword(password), new LambdaQueryWrapper<SysUser>().eq(SysUser::getId, user.getId()));
		return Result.ok("密码重置成功!");
	}

	@Override
	@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
	public Result<?> changePassword(SysUser sysUser) {
		String salt = oConvertUtils.randomGen(8);
		sysUser.setSalt(salt);
		String password = sysUser.getPassword();
		String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), password, salt);
		sysUser.setPassword(passwordEncode);
		jdbcTemplate.update("UPDATE sys_user SET password = ?, salt = ?, update_time = ?, first_login = ? WHERE id = ?"
				,sysUser.getPassword(),sysUser.getSalt(),sysUser.getUpdateTime(),sysUser.getFirstLogin(),sysUser.getId() );
		return Result.ok("密码修改成功!");
	}

	@Override
	@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteUser(String userId) {
		//1.删除用户
		this.removeById(userId);
		return false;
	}

	@Override
	@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteBatchUsers(String userIds) {
		//1.删除用户
		this.removeByIds(Arrays.asList(userIds.split(",")));
		return false;
	}

	@Override
	public SysUser getUserByName(String username) {
		// 这个方法特殊，不能使用多租户
		String sql = String.format("select * from  sys_user  where username = '%s' and del_flag = 0", username);
		System.out.println(sql);
		SysUser sysUser = jdbcTemplate.query(sql, new ResultSetExtractor<SysUser>() {
			@Override
			public SysUser extractData(ResultSet rs) throws SQLException, DataAccessException {
				if (rs.next()) {
					SysUser user = new SysUser();
					//, birthday, sexthird_id, third_type, activiti_sync, work_no, post_id, telephone, create_by,
					//create_time, update_by, update_time, user_identity,
					//depart_idsduty_names, duration_days
					user.setId(rs.getString("id"));
					user.setRealname(rs.getString("realname"));
					user.setUsername(rs.getString("username"));
					user.setPassword(rs.getString("password"));
					user.setPhone(rs.getString("phone"));
					user.setEmail(rs.getString("email"));
					user.setDelFlag(rs.getInt("del_flag"));
					user.setLicenseTime(rs.getDate("license_time"));
					user.setLicenseStatus(rs.getInt("license_status"));
					user.setIsAdmin(rs.getString("is_admin"));
					user.setIsProxy(rs.getString("is_proxy"));
					user.setStartTime(rs.getDate("start_time"));
					user.setEndTime(rs.getDate("end_time"));
					user.setSalt(rs.getString("salt"));
					user.setUserFlag(rs.getString("user_flag"));
					user.setType(rs.getString("type"));
					user.setStatus(rs.getInt("status"));
					user.setTenantId(rs.getString("tenant_id"));
					user.setShipId(rs.getString("ship_id"));
					user.setShipIdText(rs.getString("ship_id_text"));
					user.setShipType(rs.getString("ship_type"));
					user.setShipTypeName(rs.getString("ship_type_name"));
					user.setSysOrgCode(rs.getString("sys_org_code"));
					user.setSysDeptCode(rs.getString("sys_dept_code"));
					user.setSysDeptName(rs.getString("sys_dept_name"));
					user.setSubSysCode(rs.getString("sub_sys_code"));
					user.setIdCard(rs.getString("id_card"));
					user.setPost(rs.getString("post"));
					user.setPostName(rs.getString("post_name"));
					user.setAvatar(rs.getString("avatar"));
					user.setDutyIds(rs.getString("duty_ids"));
					user.setCreateTime(rs.getDate("create_time"));
					user.setUpdateTime(rs.getDate("update_time"));
					user.setFirstLogin(rs.getString("first_login"));
					return user;
				} else {
					throw new RuntimeException("用户不存在！");
				}
			}
		});
		return sysUser;
	}

	@Override
	public List<SysUser> getUserByRealName(String realname) {
		List<SysUser> userList = userMapper.getUserByRealName(realname);
		return userList;
	}

	@Override
	@Transactional
	public void addUserWithRole(SysUser user, String roles) {
		if (user.getId() != null) {
			String sql = "update sys_user set username=?,realname=?,password=?,salt=?,avatar=?,sex=?,email=?,phone=?,type=?,id_card=?, duty_ids=?,status=?,del_flag=?,work_no=?,post=?,create_time=?,sys_dept_code=?,sys_org_code=?,sub_sys_code=?, user_flag=? where id=?";
			jdbcTemplate.update(sql, user.getUsername(), user.getRealname(), user.getPassword(), user.getSalt(), user.getAvatar(), user.getSex(), user.getEmail(), user.getPhone(), user.getType(), user.getIdCard(), user.getDutyIds(), user.getStatus(), user.getDelFlag(), user.getWorkNo(), user.getPost(), user.getCreateTime(), user.getSysDeptCode(), user.getSysOrgCode(), user.getSubSysCode(), user.getUserFlag(), user.getId());
		} else {
			this.save(user);
		}
		if (oConvertUtils.isNotEmpty(roles)) {
			String[] arr = roles.split(",");
			for (String roleId : arr) {
				SysUserRole userRole = new SysUserRole(user.getId(), roleId);
				sysUserRoleMapper.insert(userRole);
			}
		}
	}

	@Override
	@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
	@Transactional
	public void editUserWithRole(SysUser user, String roles) {
		this.updateById(user);
		//先删后加
		sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, user.getId()));
		if (oConvertUtils.isNotEmpty(roles)) {
			String[] arr = roles.split(",");
			for (String roleId : arr) {
				SysUserRole userRole = new SysUserRole(user.getId(), roleId);
				sysUserRoleMapper.insert(userRole);
			}
		}
	}

	@Override
	public List<String> getRole(String username) {
		return sysUserRoleMapper.getRoleByUserName(username);
	}

	/**
	 * 通过用户名获取用户角色集合
	 *
	 * @param username 用户名
	 * @return 角色集合
	 */
	@Override
	public Set<String> getUserRolesSet(String username) {
		// 查询用户拥有的角色集合
		List<String> roles = sysUserRoleMapper.getRoleByUserName(username);
		log.info("-------通过数据库读取用户拥有的角色Rules------username： " + username + ",Roles size: " + (roles == null ? 0 : roles.size()));
		return new HashSet<>(roles);
	}

	/**
	 * 通过用户名获取用户权限集合
	 *
	 * @param username 用户名
	 * @return 权限集合
	 */
	@Override
	public Set<String> getUserPermissionsSet(String username) {
		Set<String> permissionSet = new HashSet<>();
		List<SysPermission> permissionList = sysPermissionMapper.queryByUser(username);
		for (SysPermission po : permissionList) {
//			if (oConvertUtils.isNotEmpty(po.getUrl())) {
//				permissionSet.add(po.getUrl());
//			}
			if (oConvertUtils.isNotEmpty(po.getPerms())) {
				permissionSet.add(po.getPerms());
			}
		}
		log.info("-------通过数据库读取用户拥有的权限Perms------username： " + username + ",Perms size: " + (permissionSet == null ? 0 : permissionSet.size()));
		return permissionSet;
	}

	@Override
	public SysUserCacheInfo getCacheUser(String username) {
		SysUserCacheInfo info = new SysUserCacheInfo();
		info.setOneDepart(true);
//		SysUser user = userMapper.getUserByName(username);
//		info.setSysUserCode(user.getUsername());
//		info.setSysUserName(user.getRealname());
		LoginUser user = sysBaseAPI.getUserByName(username);
		if (user != null) {
			info.setSysUserCode(user.getUsername());
			info.setSysUserName(user.getRealname());
			info.setSysOrgCode(user.getSysOrgCode());
		}
		//多部门支持in查询
		List<SysDepart> list = sysDepartMapper.queryUserDeparts(user.getId());
		List<String> sysMultiOrgCode = new ArrayList<String>();
		if (list == null || list.size() == 0) {
			//当前用户无部门
			//sysMultiOrgCode.add("0");
		} else if (list.size() == 1) {
			//sysMultiOrgCode.add(list.get(0).getSysOrgCode());
		} else {
			info.setOneDepart(false);
			for (SysDepart dpt : list) {
				//sysMultiOrgCode.add(dpt.getSysOrgCode());
			}
		}
		info.setSysMultiOrgCode(sysMultiOrgCode);
		return info;
	}

	// 根据部门Id查询
	@Override
	public IPage<SysUser> getUserByDepId(Page<SysUser> page, String departId, String username) {
		return userMapper.getUserByDepId(page, departId, username);
	}

	@Override
	public IPage<SysUser> getUserByDepIds(Page<SysUser> page, List<String> departIds, String username) {
		return userMapper.getUserByDepIds(page, departIds, username);
	}

	@Override
	public Map<String, String> getDepNamesByUserIds(List<String> userIds) {
		List<SysUserDepVo> list = this.baseMapper.getDepNamesByUserIds(userIds);
		Map<String, String> res = new HashMap<String, String>();
		list.forEach(item -> {
					if (res.get(item.getUserId()) == null) {
						res.put(item.getUserId(), item.getDepartName());
					} else {
						res.put(item.getUserId(), res.get(item.getUserId()) + "," + item.getDepartName());
					}
				}
		);
		return res;
	}

	@Override
	public IPage<SysUser> getUserByDepartIdAndQueryWrapper(Page<SysUser> page, String departId, QueryWrapper<SysUser> queryWrapper) {
		LambdaQueryWrapper<SysUser> lambdaQueryWrapper = queryWrapper.lambda();
		lambdaQueryWrapper.eq(SysUser::getDelFlag, "0");
		lambdaQueryWrapper.inSql(SysUser::getId, "SELECT user_id FROM sys_user_depart WHERE dep_id = '" + departId + "'");
		return userMapper.selectPage(page, lambdaQueryWrapper);
	}

	@Override
	public IPage<SysUserSysDepartModel> queryUserByOrgCode(String sysOrgCode, SysUser userParams, IPage page) {
		List<SysUserSysDepartModel> list = baseMapper.getUserByOrgCode(page, sysOrgCode, userParams);
		Integer total = baseMapper.getUserByOrgCodeTotal(sysOrgCode, userParams);
		IPage<SysUserSysDepartModel> result = new Page<>(page.getCurrent(), page.getSize(), total);
		result.setRecords(list);
		return result;
	}

	// 根据角色Id查询
	@Override
	public IPage<SysUser> getUserByRoleId(Page<SysUser> page, String roleId, String username) {
		return userMapper.getUserByRoleId(page, roleId, username);
	}

	@Override
	@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, key = "#username")
	public void updateUserDepart(String username, String sysOrgCode) {
		baseMapper.updateUserDepart(username, sysOrgCode);
	}

	@Override
	public SysUser getUserByPhone(String phone) {
		return userMapper.getUserByPhone(phone);
	}

	@Override
	public SysUser getUserByEmail(String email) {
		return userMapper.getUserByEmail(email);
	}

	@Override
	@Transactional
	public void addUserWithDepart(SysUser user, String selectedParts) {
//		this.save(user);  //保存角色的时候已经添加过一次了
		if (oConvertUtils.isNotEmpty(selectedParts)) {
			String[] arr = selectedParts.split(",");
			for (String deaprtId : arr) {
				SysUserDepart userDeaprt = new SysUserDepart(user.getId(), deaprtId);
				sysUserDepartMapper.insert(userDeaprt);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
	public void editUserWithDepart(SysUser user, String departs) {
		this.updateById(user);  //更新角色的时候已经更新了一次了，可以再跟新一次
		String[] arr = {};
		if (oConvertUtils.isNotEmpty(departs)) {
			arr = departs.split(",");
		}
		//查询已关联部门
		List<SysUserDepart> userDepartList = sysUserDepartMapper.selectList(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId()));
		if (userDepartList != null && userDepartList.size() > 0) {
			for (SysUserDepart depart : userDepartList) {
				//修改已关联部门删除部门用户角色关系
				if (!Arrays.asList(arr).contains(depart.getDepId())) {
					List<SysDepartRole> sysDepartRoleList = sysDepartRoleMapper.selectList(
							new QueryWrapper<SysDepartRole>().lambda().eq(SysDepartRole::getDepartId, depart.getDepId()));
					List<String> roleIds = sysDepartRoleList.stream().map(SysDepartRole::getId).collect(Collectors.toList());
					if (roleIds != null && roleIds.size() > 0) {
						departRoleUserMapper.delete(new QueryWrapper<SysDepartRoleUser>().lambda().eq(SysDepartRoleUser::getUserId, user.getId())
								.in(SysDepartRoleUser::getDroleId, roleIds));
					}
				}
			}
		}
		//先删后加
		sysUserDepartMapper.delete(new QueryWrapper<SysUserDepart>().lambda().eq(SysUserDepart::getUserId, user.getId()));
		if (oConvertUtils.isNotEmpty(departs)) {
			for (String departId : arr) {
				SysUserDepart userDepart = new SysUserDepart(user.getId(), departId);
				sysUserDepartMapper.insert(userDepart);
			}
		}
	}

	/**
	 * 校验用户是否有效
	 *
	 * @param sysUser
	 * @return
	 */
	@Override
	public Result<?> checkUserIsEffective(SysUser sysUser) {
		Result<?> result = new Result<Object>();
		//情况1：根据用户信息查询，该用户不存在
		if (sysUser == null) {
			result.error500("该用户不存在，请注册");
			sysBaseAPI.addLog("用户登录失败，用户不存在！", CommonConstant.LOG_TYPE_1, null);
			return result;
		}
		//情况2：根据用户信息查询，该用户已注销
		if (CommonConstant.DEL_FLAG_1 == sysUser.getDelFlag()) {
			sysBaseAPI.addLog("用户登录失败，用户名:" + sysUser.getUsername() + "已注销！", CommonConstant.LOG_TYPE_1, null);
			result.error500("该用户已注销");
			return result;
		}
		//情况3：根据用户信息查询，该用户已冻结
		if (CommonConstant.USER_FREEZE == sysUser.getStatus()) {
			sysBaseAPI.addLog("用户登录失败，用户名:" + sysUser.getUsername() + "已冻结！", CommonConstant.LOG_TYPE_1, null);
			result.error500("该用户已冻结");
			return result;
		}
		return result;
	}

	@Override
	public List<SysUser> queryLogicDeleted() {
		return this.queryLogicDeleted(null);
	}

	@Override
	public List<SysUser> queryLogicDeleted(LambdaQueryWrapper<SysUser> wrapper) {
		if (wrapper == null) {
			wrapper = new LambdaQueryWrapper<>();
		}
		wrapper.eq(SysUser::getDelFlag, "1");
		return userMapper.selectLogicDeleted(wrapper);
	}

	@Override
	public SysUser getByIdLogicDeleted(String id) {
		return userMapper.selectByIdLogicDeleted(id);
	}

	@Override
	public boolean revertLogicDeleted(List<String> userIds, SysUser updateEntity) {
		String ids = String.format("'%s'", String.join("','", userIds));
		return userMapper.revertLogicDeleted(ids, updateEntity) > 0;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean removeLogicDeleted(List<String> userIds) {
		String ids = String.format("'%s'", String.join("','", userIds));
		// 1. 删除用户
		int line = userMapper.deleteLogicDeleted(ids);
		// 2. 删除用户部门关系
		line += sysUserDepartMapper.delete(new LambdaQueryWrapper<SysUserDepart>().in(SysUserDepart::getUserId, userIds));
		//3. 删除用户角色关系
		line += sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId, userIds));
		return line != 0;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateNullPhoneEmail() {
		userMapper.updateNullByEmptyString("email");
		userMapper.updateNullByEmptyString("phone");
		return true;
	}

	@Override
	public void saveThirdUser(SysUser sysUser) {
		//保存用户
		String userid = UUIDGenerator.generate();
		sysUser.setId(userid);
		baseMapper.insert(sysUser);
		//获取第三方角色
		SysRole sysRole = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleCode, "third_role"));
		//保存用户角色
		SysUserRole userRole = new SysUserRole();
		userRole.setRoleId(sysRole.getId());
		userRole.setUserId(userid);
		sysUserRoleMapper.insert(userRole);
	}

	@Override
	public List<SysUser> queryByDepIds(List<String> departIds, String username) {
		return userMapper.queryByDepIds(departIds, username);
	}

	@Override
	public SysUser getUserByThirdId(String thirdId) {
		return userMapper.getUserByThirdId(thirdId);
	}

	@Override
	@CacheEvict(value = {CacheConstant.SYS_USERS_CACHE}, allEntries = true)
	public void changeShipType(String shipType) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		UpdateWrapper updateWrapper = new UpdateWrapper();
		updateWrapper.set("ship_type", shipType);
		updateWrapper.eq("id", loginUser.getId());
		this.update(updateWrapper);
	}

	@Override
	public String getCompanyCode() {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		return sysUser.getTenantId();
	}

	@Override
	public Boolean checkUserName(String username, String id) {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("username", username);
		queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
		if (id != null && id != "") {
			queryWrapper.ne("id", id);
		}
		Long count = userMapper.selectCount(queryWrapper);
		if (count > 0) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public List<Map<String, String>> normalUserName() {
		QueryWrapper queryWrapper = new QueryWrapper();
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//		queryWrapper.likeRight("sys_org_code",sysUser.getSysOrgCode());
		queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
		queryWrapper.eq("status", CommonConstant.DEL_FLAG_1);
		List<SysUser> departs = userMapper.selectList(queryWrapper);
		List<Map<String, String>> dataList = Lists.newArrayList();
		for (SysUser user : departs) {
			Map<String, String> map = Maps.newHashMap();
			map.put("label", user.getRealname());
			map.put("value", user.getId());
			dataList.add(map);
		}
		return dataList;
	}

	@Override
	public List<SysDepartTreeModel> queryTreeList() {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
		query.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		//query.likeRight(SysDepart::getId,sysUser.getDepartIds());
		query.ne(SysDepart::getStatus, "0");
		query.orderByAsc(SysDepart::getDepartOrder);
		List<SysDepart> list = sysDepartMapper.selectList(query);
		// 调用wrapTreeDataToTreeList方法生成树状数据
		List<SysDepartTreeModel> listResult = FindsDepartsChildrenUtil.wrapTreeDataToTreeList(list);
		return listResult;
	}

	@Override
	public Boolean checkIdCard(String idCard, String id) {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("id_card", idCard);
		queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
		if (id != null && id != "" && !id.equals("undefined")) {
			queryWrapper.ne("id", id);
		}
		Long count = userMapper.selectCount(queryWrapper);
		if (count > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<SysUser> getUserPageList(Integer currIndex, Integer pageSize) {
		return sysUserMapper.pageUserList(currIndex, pageSize);
	}

	@Override
	public void consent(String username) {
		SysUser user = sysUserMapper.getUserByName(username);
		user.setLicenseStatus(1);
		user.setLicenseTime(new Date());
		sysUserMapper.updateById(user);
	}

	@Override
	public Boolean checkUserExist(String userName) {
		//通过传入信息查询新的用户信息
		String sql = String.format("select * from  sys_user  where username = '%s' and del_flag = 0", userName);
		List<Map<String, Object>> userList = jdbcTemplate.queryForList(sql);
		return userList.size() > 0;
	}

	@Override
	public List<SysUser> selectDeptUser(List<String> deptCode) {
		List<SysUser> sysUsers = new ArrayList<>();
		deptCode.forEach(item->{
			List<SysUser> sysUser =sysUserMapper.selectDeptUser(item);
			sysUsers.addAll(sysUser);
		});
		return sysUsers;
	}
}
